#tag Class
Protected Class GameCanvas
Inherits Canvas
	#tag Event
		Sub Paint(g As Graphics)
		  dim i as integer
		  
		  for i = 0 to ubound(tiles)
		    tiles(i).draw
		  next
		  
		  for i = 0 to UBound(currentPossiblePositions)
		    winMain.canBoard.Graphics.ForeColor = &c80FF00
		    winMain.canBoard.Graphics.FillRect (GetXRelativeToStartTile(currentPossiblePositions(i).x), _
		    GetYRelativeToStartTile(currentPossiblePositions(i).y), medievo.TILESIZE, medievo.TILESIZE)
		  next
		  
		  DrawGrid
		End Sub
	#tag EndEvent

	#tag Event
		Sub MouseMove(X As Integer, Y As Integer)
		  
		  dim xCoord as integer
		  dim yCoord as integer
		  dim absolutePos as Coordinates
		  dim pos as Coordinates
		  dim i as integer
		  
		  if winMain.facade.GameExists = true then
		    if winMain.mode = 1 then //mode 1 = game, mode 2 = aiviewer
		      
		      if (x < lastPos.x or x > lastpos.x + app.TILESIZE) or _
		        (y < lastPos.y or y > lastPos.y + app.TILESIZE) then
		        
		        me.Graphics.ClearRect (0,0, self.Width,self.Height)
		        me.Graphics.PenWidth = 1
		        
		        me.Refresh
		        
		        'this can also made better...
		        xCoord = x/app.TILESIZE
		        yCoord = y/app.TILESIZE
		        pos.x = xCoord * app.TILESIZE
		        pos.y = yCoord * app.TILESIZE
		        
		        
		        
		        try
		          me.Graphics.DrawPicture winMain.facade.GetCurrentTile.GetPicture, pos.x, pos.y, app.TILESIZE, app.TILESIZE
		        catch e as NilObjectException
		          
		        end try
		        
		        absolutePos = GetAbsolutePositionByXy(x,y)
		        
		        me.Graphics.ForeColor = &c66CCFF
		        me.Graphics.PenWidth = 8
		        me.Graphics.DrawRect xCoord * app.TILESIZE, yCoord * app.TILESIZE, app.TILESIZE, app.TILESIZE
		        
		      end if
		    end if
		  end if
		  
		  lastpos = UpdatePositionState(x,y)
		  
		End Sub
	#tag EndEvent

	#tag Event
		Function MouseDown(X As Integer, Y As Integer) As Boolean
		  if winMain.mode = 1 then
		    
		    winMain.facade.AddTileAtPosition(x,y)
		    
		  end if
		End Function
	#tag EndEvent


	#tag Method, Flags = &h0
		Sub DrawGrid()
		  dim xVal as double
		  dim yVal as double
		  
		  xVal = 0
		  yVal = 0
		  
		  me.Graphics.PenWidth = 1
		  me.Graphics.ForeColor = &c808080
		  
		  // horizontal lines
		  while xVal < self.Width
		    me.Graphics.DrawLine xVal, yVal, xVal, self.Height
		    xVal = xVal + app.TILESIZE
		  wend
		  
		  xVal = 0
		  yVal = 0
		  
		  //vertical lines
		  while yVal < self.Width
		    me.Graphics.DrawLine xVal, yVal, self.Width, yVal
		    yVal = yVal + app.TILESIZE
		  wend
		  
		  me.Graphics.PenWidth = 2
		  me.Graphics.ForeColor = &c000000
		  
		  //bounding rectangle
		  //me.Graphics.DrawRect 0, 0, me.Width, me.Height
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h21
		Private Function UpdatePositionState(x as integer, y as integer) As coordinates
		  dim absolutePosition as Coordinates
		  dim relativePosition as Coordinates
		  
		  absolutePosition = GetAbsolutePositionByXy(x,y)
		  relativePosition = GetXyRelativeToStartTile(absolutePosition)
		  
		  
		  
		  'somewhere else at the gameboard
		  winMain.txtState.Text = "Pos(x/y): " + str(absolutePosition.x) +  "/" + str(absolutePosition.y) _
		  + " Rel(x/y): " + str(relativePosition.x) + "/" + str(relativePosition.y)
		  
		  return relativePosition
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub SetTiles(tiles() as Tile)
		  self.tiles = tiles
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h21
		Private Function GetYRelativeToStartTile(y as integer) As integer
		  return floor((winMain.canBoard.Height / medievo.TILESIZE) / 2) * medievo.TILESIZE _
		  + winmain.xyScroll.y - y * medievo.TILESIZE
		End Function
	#tag EndMethod

	#tag Method, Flags = &h21
		Private Function GetXRelativeToStartTile(x as integer) As integer
		  return floor((winMain.canBoard.Width / medievo.TILESIZE) /  2) * medievo.TILESIZE _
		  + winMain.xyScroll.x + x * medievo.TILESIZE
		End Function
	#tag EndMethod


	#tag Property, Flags = &h0
		startX As Integer
	#tag EndProperty

	#tag Property, Flags = &h0
		startY As Integer
	#tag EndProperty

	#tag Property, Flags = &h0
		tiles() As Tile
	#tag EndProperty

	#tag Property, Flags = &h0
		currentPossiblePositions() As coordinates
	#tag EndProperty

	#tag Property, Flags = &h21
		Private lastPos As Coordinates
	#tag EndProperty


	#tag ViewBehavior
		#tag ViewProperty
			Name="Name"
			Visible=true
			Group="ID"
			Type="String"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Index"
			Visible=true
			Group="ID"
			Type="Integer"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Super"
			Visible=true
			Group="ID"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="ControlOrder"
			Visible=true
			Group="Position"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Left"
			Visible=true
			Group="Position"
			Type="Integer"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Top"
			Visible=true
			Group="Position"
			Type="Integer"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Width"
			Visible=true
			Group="Position"
			InitialValue="100"
			Type="Integer"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Height"
			Visible=true
			Group="Position"
			InitialValue="100"
			Type="Integer"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="LockLeft"
			Visible=true
			Group="Position"
			Type="Boolean"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="LockTop"
			Visible=true
			Group="Position"
			Type="Boolean"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="LockRight"
			Visible=true
			Group="Position"
			Type="Boolean"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="LockBottom"
			Visible=true
			Group="Position"
			Type="Boolean"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="TabPanelIndex"
			Group="Position"
			InitialValue="0"
			Type="Integer"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Visible"
			Visible=true
			Group="Appearance"
			InitialValue="True"
			Type="Boolean"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="HelpTag"
			Visible=true
			Group="Appearance"
			Type="String"
			EditorType="MultiLineEditor"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="AutoDeactivate"
			Visible=true
			Group="Appearance"
			InitialValue="True"
			Type="Boolean"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Enabled"
			Visible=true
			Group="Appearance"
			InitialValue="True"
			Type="Boolean"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="UseFocusRing"
			Visible=true
			Group="Appearance"
			InitialValue="True"
			Type="Boolean"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Backdrop"
			Visible=true
			Group="Appearance"
			Type="Picture"
			EditorType="Picture"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="AcceptFocus"
			Visible=true
			Group="Behavior"
			Type="Boolean"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="AcceptTabs"
			Visible=true
			Group="Behavior"
			Type="Boolean"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="EraseBackground"
			Visible=true
			Group="Behavior"
			InitialValue="True"
			Type="Boolean"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="InitialParent"
			InheritedFrom="Canvas"
		#tag EndViewProperty
		#tag ViewProperty
			Name="startX"
			Group="Behavior"
			InitialValue="0"
			Type="Integer"
		#tag EndViewProperty
		#tag ViewProperty
			Name="startY"
			Group="Behavior"
			InitialValue="0"
			Type="Integer"
		#tag EndViewProperty
	#tag EndViewBehavior
End Class
#tag EndClass
